import $ from '../common';
Page({
	data: {
		phone: ""
	},
	submit(e) {
		var value = e.detail.value
		if(!value.phone) {
			$.tip("手机号不能为空！")
			return false
		}
		if((value.passwd1 == value.passwd2) && (value.passwd1 && value.passwd2)) {
			$.post("/passwd/reset/" + value.phone, value, (data) => {
				$.alert("更改成功!请使用新密码重新登录~", function() {
					wx.removeStorageSync('token')
					wx.removeStorageSync('uuid')
					$.navigateTo("/login/index", 1)
				})
			})
		} else {
			$.tip("两次密码需要相同且不能为空！")
		}
	},
	countdown: 60,
	bindPhone(e) {
		this.data.phone = e.detail.value
	},
	resetDentify() {
		clearInterval(this.setTimeDentify)
		this.disabledCode = false
		this.countdown = 60
		this.setData({
			countdown: this.countdown,
			disabledCode: this.disabledCode
		})
	},
	getIdentify() {
		$.post("/verifycode", {
			phone: this.data.phone,
			channel:'开班神器'
		}, (data) => {
			$.tip("验证码已发送到尾号为"+ (this.data.phone).toString().substring(7) +"的手机上，请注意查收!")
		}, () => {
			this.resetDentify()
		})
	},
	getCode() {
		let countdown = this.countdown
		let _fn = () => {
			if(countdown == 0) {
				this.resetDentify()
			} else {
				this.disabledCode = true
				this.setData({
					countdown,
					disabledCode: this.disabledCode
				})
				if(countdown == 60) {
					this.getIdentify()
				}
				countdown--
			}
		}
		_fn()
		this.setTimeDentify = setInterval(() => {
			_fn()
		}, 1000)
	},
	onLoad(e) {
		let $change = false
		this.data.phone = e.phone
		e.phone && [$change = true]
		this.setData({
			phone: this.data.phone,
			$change: $change
		})
	}
});